# -*- coding: utf-8 -*-

from datetime import datetime
from typing import List


class Stack(object):
    # init empty stack
    def __init__(self):
        self.items = []

    # is empty stack
    def is_empty(self):
        return self.items == []

    # get the top element
    def peek(self):
        return self.items[len(self.items) - 1]

    # get size of the stack
    def size(self):
        return len(self.items)

    # push one element）
    def push(self, item):
        self.items.append(item)

    # pop the top element
    def pop(self):
        return self.items.pop()


def is_comment_line(line: str) -> bool:
    if '//' in line:
        return True
    line = line.strip()
    if line.startswith('/*') or line.startswith('*'):
        return True
    return False


def is_log_line(line: str) -> bool:
    log_level_mark = ['.info', '.trace', '.warn', '.error', '.debug']
    for item in log_level_mark:
        if item in line:
            return True
    return False


def make_document_comment(author: str) -> str:
    lines = list()
    lines.append('/**')
    lines.append(f' * @author {author}')
    now_str = str(datetime.now())
    lines.append(f' * @date {now_str[:now_str.index(".")]}')
    lines.append(' */')
    return '\n'.join(lines)


def find_bracket_idx(line: str) -> List[int]:
    """
    returns the start index and end index of bracket pair
    for example:
        line = 'toIce(domain);'
        returns [5, 12]

        line = 'toIce(Collectors.toList());'
        returns [5, 25]
    returns [] if not found
    """
    bracket_pair = []
    if '(' in line:
        bracket_stack = Stack()

        start = line.index('(')
        bracket_stack.push('(')
        bracket_pair.append(start)
        n = len(line)
        for i in range(start + 1, n):
            item = line[i]
            if line[i] == ')':
                if bracket_stack.peek() == '(':
                    bracket_stack.pop()
            elif line[i] == '(':
                bracket_stack.push('(')

            if bracket_stack.is_empty():
                bracket_pair.append(i)
                break

    return bracket_pair


def rightest_word(line: str) -> str:
    """
    return the rightest word
    :param line: such as "return ClassPosition"
    :return: ClassPosition
    """
    res_str = ''
    line = line.strip()
    for idx in range(len(line)-1, -1, -1):
        if line[idx].isalpha() or line[idx].isnumeric():
            res_str = line[idx] + res_str
        else:
            break
    return res_str
